package com.devin.avatar.common.entity;

import java.util.HashMap;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.devin.avatar.common.config.Global;
import com.devin.avatar.common.util.CookieUtils;

public class RequestParams extends HashMap<String, Object> {
    private static final long serialVersionUID = 1L;

    private int pageNum = 1; // 当前页码
    private int pageSize = Integer.valueOf(Global.getConfig("page.pageSize")); // 页面大小，设置为“-1”表示不进行分页（分页无效）
    private String orderBy = ""; // 标准查询有效， 实例： updatedate desc, name asc

    /**
     * 构造方法
     * 
     * @param request 传递 repage 参数，来记住页码
     * @param response 用于设置 Cookie，记住页码
     */
    public RequestParams(HttpServletRequest request, HttpServletResponse response) {
	// 设置页码参数（传递repage参数，读取cookie页码）
	String pageNum = request.getParameter("pageNum");
	if (StringUtils.isNumeric(pageNum)) {
	    CookieUtils.setCookie(response, "pageNum", pageNum);
	    this.setPageNum(Integer.parseInt(pageNum));
	} else if (request.getParameter("repage") != null) {
	    pageNum = CookieUtils.getCookie(request, "pageNum");
	    if (StringUtils.isNumeric(pageNum)) {
		this.setPageNum(Integer.parseInt(pageNum));
	    }
	}

	// 设置页面大小参数（传递repage参数，读取cookie页面大小）
	String size = request.getParameter("pageSize");
	if (StringUtils.isNumeric(size)) {
	    CookieUtils.setCookie(response, "pageSize", size);
	    this.setPageSize(Integer.parseInt(size));
	} else if (request.getParameter("repage") != null) {
	    pageNum = CookieUtils.getCookie(request, "pageSize");
	    if (StringUtils.isNumeric(size)) {
		this.setPageSize(Integer.parseInt(size));
	    }
	}

	// 设置排序参数
	String orderBy = request.getParameter("orderBy");
	if (StringUtils.isNotBlank(orderBy)) {
	    this.setOrderBy(orderBy);
	}
    }

    /**
     * 获取当前页码
     * 
     * @return
     */
    public int getPageNum() {
	return pageNum;
    }

    /**
     * 设置当前页码
     * 
     * @param pageNum
     */
    public void setPageNum(int pageNum) {
	this.pageNum = pageNum;
    }

    /**
     * 获取页面大小
     * 
     * @return
     */
    public int getPageSize() {
	return pageSize;
    }

    /**
     * 设置页面大小（最大500）
     * 
     * @param pageSize
     */
    public void setPageSize(int pageSize) {
	this.pageSize = pageSize <= 0 ? 10 : pageSize;// > 500 ? 500 : pageSize;
    }

    /**
     * 设置查询排序，标准查询有效， 实例： updatedate desc, name asc
     */
    public void setOrderBy(String orderBy) {
	this.orderBy = orderBy;

	// SQL过滤，防止注入
	String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|"
		+ "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
	Pattern sqlPattern = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
	if (sqlPattern.matcher(orderBy).find()) {
	    this.put("orderBy", "");
	} else {
	    this.put("orderBy", this.orderBy);
	}
    }
}
